package com.example.springboot.controller;

import com.example.springboot.pojo.Book;
import com.example.springboot.pojo.Department;
import com.example.springboot.pojo.Staff;
import com.example.springboot.service.DepartmentService;
import com.example.springboot.service.StaffService;
import net.sf.json.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@org.springframework.stereotype.Controller
@RequestMapping("/staff")
@CrossOrigin
public class StaffController {
    @Autowired
    private StaffService staffService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 接口测试用
     * 获取所有的员工信息
     * @return List<Staff>
     */
//    @ResponseBody
//    @RequestMapping("/getStaff")
//    public List<Staff> getStaff(){
//        return staffService.selectAllStaff();
//    }


    /**
     * 用于在前端界面显示
     * 由前端js进行数据读取
     * @return JSONArray
     */
    @ResponseBody
    @RequestMapping("/getStaff")
    public JSONArray getStaff(){
        List<Staff> staff = staffService.selectAllStaff();
        JSONArray json = JSONArray.fromObject(staff);
        return json;
    }

    /**
     * 获取部门列表
     * @return JSONArray
     */
    @ResponseBody
    @RequestMapping("/getDept")
    public JSONArray getDept(){
        List<Department> departments = departmentService.selectAllDept();
        JSONArray json = JSONArray.fromObject(departments);
        return json;
    }

    /**
     * 按条件查询
     * 支持模糊查询
     * @param id 员工id
     * @param name 员工姓名
     * @param dept 部门
     * @return
     */
    @ResponseBody
    @RequestMapping("/searchStaff")
    public JSONArray searchStaff(@Param("id") Integer id, @Param("name") String name, @Param("gender") String gender, @Param("dept") Integer dept){
        List<Staff> staff = staffService.selectStaff(id, name, gender, dept);
        JSONArray json = JSONArray.fromObject(staff);
        return json;
    }

    /**
     * 添加新员工
     * @param name
     * @param gender
     * @param dept
     */
    @ResponseBody
    @RequestMapping("/addStaff")
    public void addStaff(@Param("name") String name, @Param("gender") String gender, @Param("dept") Integer dept){
        Staff staff = new Staff(null, name, gender, dept, null);
        staffService.insertStaff(staff);
    }

    /**
     * 删除员工
     * @param id
     */
    @ResponseBody
    @RequestMapping("/delStaff")
    public void delStaff(@Param("id") Integer id){
        staffService.deleteStaff(id);
    }


    /**
     * 编辑员工
     */
    @ResponseBody
    @RequestMapping("/updateStaff")
    public void updateStaff(Staff staff){
        staffService.updateStaff(staff);
    }
}
